package strategy.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
public class StrategyConfig {
    /**
     * 返回类型为Map<String,Bean> 时， 一定要手动定义Bean的名称
     * @param strategyList  由spring框架自动注入，所有实现了 Strategy 的对象
     * @return
     */
    @Bean("strategyContextMap")
    public Map<String, Strategy> getMap(List<Strategy> strategyList) {
        Map<String, Strategy> map = new HashMap<>();
        for(Strategy entry : strategyList){
            StrategyType annotation = AnnotationUtils.findAnnotation(entry.getClass(), StrategyType.class);
            map.put(annotation.value(), entry);
        }
        return map;
    }
}
